home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / aztcmtsk.arc / AZTASK.CRT next >
Text File  |  1986-03-24  |  3KB  |  139 lines

  1. /* Copyright (C) 1981,1982 by Manx Software Systems */
  2. /* Copyright (C) 1982  Thomas Fenwick */
  3. /* edited 8/21/85 by DECortesi in order to:
  4.     1. allow " specials and blanks" in argv tokens,
  5.     2. eliminate attempted unlink of $$$.SUB on
  6.         normal program exit (i.e. termination of
  7.         main() without an explicit return(code)),
  8.     3. extend exit(code) to check for CP/M PLUS and,
  9.         if found, to set the program return code
  10.         and to cancel submit files its way,
  11.     4. fix abysmal performance of redirected output
  12.         with an insertion from James Apedaile.
  13. */
  14. /* edited 3/86 by DECortesi to make into TROOT.C, a tasking
  15.     CROOT.    Croot() now sets up main(argc,argv) as a task
  16.     with a stack of 256 words, invoked by calling defer().
  17. 3/13/86 correct order of main() arguments
  18. */
  19. #include "errno.h"
  20. #include "fcntl.h"
  21. #include "io.h"
  22. #include "stdio.h"    /* added by Apedaile fix */
  23.  
  24. /*
  25.  channel table: relates fd's to devices
  26. */
  27. badfd()
  28. {
  29.     errno = EBADF;
  30.     return -1;
  31. }
  32. noper()
  33. {
  34.     return 0;
  35. }
  36. struct channel    chantab[] = {
  37.     { 2, 0, 1, 0, noper, 2 },
  38.     { 0, 2, 1, 0, noper, 2 },
  39.     { 0, 2, 1, 0, noper, 2 },
  40.     { 0, 0, 0, 0, badfd, 0 },
  41.     { 0, 0, 0, 0, badfd, 0 },
  42.     { 0, 0, 0, 0, badfd, 0 },
  43.     { 0, 0, 0, 0, badfd, 0 },
  44.     { 0, 0, 0, 0, badfd, 0 },
  45.     { 0, 0, 0, 0, badfd, 0 },
  46.     { 0, 0, 0, 0, badfd, 0 },
  47.     { 0, 0, 0, 0, badfd, 0 },
  48. };
  49.  
  50. #define MAXARGS 30
  51. static char *Argv[MAXARGS];
  52. static char Argbuf[128];
  53. static int Argc;
  54.  
  55.  
  56. exit(code)    /* extensively revised */
  57. {
  58. register int sysno;
  59. static int killget = 0x81;
  60.     closall_();
  61.     if ( code ) {
  62.         sysno = bdos(12,0);
  63.          if (sysno < 0x0030) /* CP/M 2.2 or less */
  64.             unlink("A:$$$.SUB");    /* cancel submit */
  65.         else if (sysno < 255)
  66.         { /* CP/M Plus */
  67.             bdos(108,code); /* set program retcode */
  68.             bdos(60,&killget); /* kill GET rsx */
  69.         } /* else MP/M */
  70.     }
  71.     boot_();
  72. }
  73.  
  74. extern main();
  75.  
  76. Croot()
  77. {
  78.     register char *cp, *fname;
  79.     register int k;
  80.  
  81.     blockmv(Argbuf, (char *)0x81, 127);
  82.     Argbuf[*(char *)0x80 & 0x7f] = 0;
  83.     Argv[0] = "";
  84.     cp = Argbuf;
  85.     Argc = 1;
  86.     while (Argc < MAXARGS) {
  87.         while (*cp == ' ' || *cp == '\t')
  88.             ++cp;
  89.         if (*cp == 0)
  90.             break;
  91.         if (*cp == '>') {        /* redirect output */
  92.             k = 1;
  93.             goto redirect;
  94.         } else if (*cp == '<') {    /* redirect input */
  95.             k = 0;
  96. redirect:
  97.             while (*++cp == ' ' || *cp == '\t')
  98.                 ;
  99.             fname = cp;
  100.             while (*++cp)
  101.                 if (*cp == ' ' || *cp == '\t') {
  102.                     *cp++ = 0;
  103.                     break;
  104.                 }
  105.             close(k);
  106.             if (k)
  107.                  {
  108.      /* inserted by Apedaile */ Cbuffs[k]._buflen = BUFSIZ;
  109.                 k = creat(fname, 0666);
  110.                  }
  111.             else
  112.                 k = open(fname, O_RDONLY);
  113.             if (k == -1) {
  114.                strcpy(0x80, "Can't open file for redirection: ");
  115.                 strcat(0x80, fname);
  116.                 strcat(0x80, "\n");
  117.                 write(2, 0x80, strlen(0x80));
  118.                 exit(10);
  119.             }
  120. /* new case */    } else if (*cp == '\"') {
  121. /* inserted to */    Argv[Argc++] = cp+1;
  122. /* handle quoted */    while (*++cp)
  123. /* tokens: take */        if (*cp=='\"') {
  124. /* all of token up to */        *cp++ = 0;
  125. /* closing quote or end */        break;
  126. /* of line */            }
  127.         } else {
  128.             Argv[Argc++] = cp;
  129.             while (*++cp)
  130.                 if (*cp == ' ' || *cp == '\t') {
  131.                     *cp++ = 0;
  132.                     break;
  133.                 }
  134.         }
  135.     }
  136.     maketask(&main,256,2,Argc,Argv); /* sets up main task */
  137.     defer(); /* invokes it, no return */
  138. }
  139.